var pageii = null;
var chek = 'ROOT';
var grid = null;
$(function() {
	grid = lyGrid({
		id : 'paging',
		l_column : [ {
			colkey : "function_id",
			name : "id",
			hide : true
		}, {
			colkey : "function_name",
			name : "菜单名称",
			align : 'left'
		}, {
			colkey : "function_type",
			name : "菜单类型",
			width : "70px",
			renderData : function(rowindex, data, rowdata, column) {
				if(data=="1"){
					return "目录";
				}else if(data=="2"){
					return "菜单";
				}else if(data=="3"){
					return "按钮";
				}
			}
		}, {
			colkey : "function_code",
			name : "菜单标识"
		}, {
			colkey : "url",
			name : "URL地址",
			align : "left",
		},
		{
			colkey : "squence",
			name : "排序"
		},
		{
			colkey : "state",
			name : "是否隐藏",
			renderData : function(rowindex, data, rowdata, column) {
				if(data=="0"){
					return "否";
				}else if(data=="1"){
					return "是";
				}
			}
		}],
		jsonUrl : rootPath + '/system/function/functions.shtml?parent_id=ROOT',
		checkbox : true,
//		usePage : false	,
		singleCheck:true,
		checkValue : 'function_id' 
			
		});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addFun").click("click", function() {
		addFun();
	});
	$("#editFun").click("click", function() {
		editFun();
	});
	$("#delFun").click("click", function() {
		delFun();
	});
	$("#lyGridUp").click("click", function() {// 上移
		lyGridUp();
	});
	$("#lyGridDown").click("click", function() {// 下移
		lyGridDown();
	});
	
	$("#addRole").click("click", function() {// 下移
		addRole();
	});
	
	loadFunctionTree();	
	
});
function editFun() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		  tips("请选中需要编辑的项",1);
		return;
	}
	showModal("function_info","编辑", rootPath + "/system/function/editUI.shtml?id=" + cbox,680,450);
	
	
}
function addFun() {
	var pid = $("#parent_id").val();
	if(!pid||pid==''){
		tips("请选择左侧菜单",1);
		return;
	}
	$.ajax({
		type : 'POST',
		async : false,
		data : {id:pid},
		url : rootPath + "/system/function/queryLevel.shtml",
		dataType : 'json',
		success : function(data) {
			if(data==false){
				tips("已是最底层",1);
				return ;
			}else {
				showModal("function_info","新增", rootPath + "/system/function/addUI.shtml?functionId="+pid,680,450);
			}
		}
	});
}
function delFun() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		tips("请选择需要删除的项",1);
		return;
	}
	confirm('你确定要删除吗？', function() {
		var url = rootPath + '/system/function/deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if(s=="success"){
			tips("操作成功",1);
			loadData();
		}else{
			tips("操作失败",1);
		}
	});
}

function lyGridUp() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.alert("请选择上移项！！");
		return;
	}
	
	layer.confirm('是否上移？', function(index) {
		var url = rootPath + '/system/function/sortUpdate.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('上移成功');
			grid.loadData();
		}
		else if(s =="maxNum"){
			layer.msg('已是最大值');
		}
		else {
			layer.msg('上移失败');
		}
	});

}


function lyGridDown() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.alert("请选择下移项！！");
		return;
	}
	
	layer.confirm('是否下移？', function(index) {
		var url = rootPath + '/system/function/sortDown.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('下移成功');
			grid.loadData();
		}
		else if(s =="minNum"){
			layer.msg('已是最小值');
		}
		else {
			layer.msg('下移失败');
		}
	});

}


/*关联角色*/
function addRole(){
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		 tips('请选择需要关联角色的数据',1);
		return;
	}
	showModal("function","关联角色", rootPath + "/system/function/roleUI.shtml?functionId="+cbox,750,350);
}


//重新加载数据
function loadData(){
	var searchParams = $("#searchForm").serializeJson();// 初始化传参数
	grid.setOptions({
		data : searchParams
	});
	loadFunctionTree();
}



//点击跳转
function checkNode(data){
	chek = data.id;
	$("#parent_id").val(chek);
	grid.setOptions({								 
		jsonUrl :  rootPath + '/system/function/functions.shtml?parent_id='+chek
	});
}

function loadFunctionTree(){
	//加载树
	$("#functionTreePanel").load(rootPath+"/system/function/tree.shtml");
}

function confirm(title,yes,no){
	 Wind.use("artDialog", "iframeTools", function () {
		art.dialog.confirm(title, function () {
			if(yes){
				yes()
			}
		}, function () {
			if(no){
				no()
			}
		});
	 });
}

function showModal(id,title, url,width,height) {
    Wind.use("artDialog", "iframeTools", function () {
        art.dialog.open(url, {
            title: title,
            id: id,
            width: width,
            height: height,
            lock: true,
            fixed: true,
            background: "#CCCCCC",
            opacity: 0.7
        });
    });
}




